<?php
	require_once("include/common.php");
?>



<!DOCTYPE html>
<html lang="en" ng-app="deviceApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">

<title>用户数据</title>

	<?php
		require_once("pages/resources.php");
	?>
<!-- 
	<link href="/meter/resources/bootstrap-3.3.6/css/bootstrap.min.css" rel="stylesheet">
 -->
	<script src="/meter/resources/jquery/jquery-2.0.3.js"></script>
	<script src="/meter/resources/js/jquery.jeditable.lk.js"></script>
 	<script src="/meter/resources/angular-1.5.5/angular.js"></script>
 	<script src="/meter/resources/js-base-util.js"></script>
 
</head>
<body class="sidebar-narrow"  id="deviceCtrl" ng-controller="deviceCtrl">

	<!-- Navbar -->
	 
	<?php
		require_once("pages/navbar.php");
	?>
	<!-- /navbar -->


	<div class="page-container sidebar-hidden">

		<!-- Sidebar -->
		 
		<?php
			require_once("pages/siderbar.php");
		?>
		<!-- /sidebar -->


		<!-- Begin: Content -->
		<div class="page-content">
			 
			<div class="row" style="margin-top:10px; margin-bottom: 24px">
				<div style="float: left; width: 10%">
	                <div class="input-group">
	                    <span class="input-group-addon"> 用户名称 </span>
	                    <input type="text" class="form-control" value="{{user}}" 
	                                readonly="readonly" 
	                                 >
	                </div>  	
	                
	            </div>

				<div style="float: left; width: 40%">
	                <div class="input-group">
	                      <span class="input-group-addon" id="basic-addon2"> 用户名称 </span>
	                  	<input type="text" class="form-control" placeholder="Search"
	                                ng-model="uq"
	                                ng-change="user_change()" aria-describedby="basic-addon2">
	                </div>
	            </div> 
	          
	            <div style="float: right; width: 40%;">
	                <div class="input-group">
	                      <span class="input-group-addon" id="basic-addon1"> 电表名称 </span>
	                  <input type="text" class="form-control" placeholder="Search"
	                                ng-model="q"
	                                ng-change="change()" aria-describedby="basic-addon1">
	                </div>
	            </div> 
	        </div>
	        <div class="row">
	        	<div style="float: left; width: 22%; ">
	            	<table class="table table-striped table-bordered" >
						<thead>
							<tr>
								<td> 用户标示  </td>
								 
						    </tr>
						</thead>
						<tbody>

							<tr ng-repeat="u in users">
					    		<td ng-style="{'background-color': u._USERID==user ? 'lightgreen':''}" >
					    			<a href="#" ng-click="changeUser(u._USERID)">
					    			{{u._NAME}}( {{u._USERID}}  )
					    			</a>
					    		</td>
					    		 
					    	</tr>
						</tbody>					
					</table>

				 
				</div> 
				<div style="width: 75%; float: right">
            	<table class="table table-striped table-bordered" >
					<thead>
						<tr>
							<td> 绑定关系  </td>
							<td> 表计代码  </td>
							<td> 地址 </td>
							 
							<td> 当前使用者  </td>

					    </tr>
					</thead>
					<tbody>

						<tr ng-repeat="dev in devices">
							<td> <input type="checkbox" class="styled" id="checkbox{{dev._METER}}" ng-checked="user==dev._USERID" ng-click="updateMeter( dev._METER );"> </td>
				    		<td> <a href="meter-data.php?meter={{dev._METER}}" >{{dev._METER}}</a></td>
				    		<td>{{dev._NAME}}</td>
				    		 
				    		<td><a href="user-data.php?user={{dev._USERID}}" > {{dev._USER}} </a></td>

				    	</tr>
					</tbody>					
				</table>
			</div> 
	        </div>
	        
			

		</div>
		<!-- End: Content -->

 			
		 <!-- Footer -->
	         
        <?php
			require_once("pages/footer.php");
		?>
        <!-- /footer -->

	</div>

	<div style="position: fixed; bottom: 0px ; right: 0px; z-index: 999">
             
        <span id="responseId" class="label ">
            success!
        </span>
      
    </div>

</body>
<script type="text/javascript">

	var users =  null;

	function response_status( command , result ){
		if(result.status == 0 ){
			$('#responseId').removeClass('label-warning');
			$('#responseId').addClass('label-success');
			$('#responseId').text( 'success! ' + command);
		}else {
			$('#responseId').removeClass('label-success');
			$('#responseId').addClass('label-warning');
			
			$('#responseId').text( 'Error : ' + result.status + ' ' + result.message + '\n' + command );
		}
	}
	
	 

    var app = angular.module('deviceApp', []);
    app.controller('deviceCtrl', 
    	function($scope, $http, $interval) {
    		$scope.uq = '';
        	$scope.q = '' ;
         	$scope.user = null;
        	$scope.users =  [] ; 
    
            $scope.getMeter = function(meterId){
            	for (var i = 0; i < $scope.devices.length; i++) {
            		if( meterId == $scope.devices[i]._METER) {
            			return $scope.devices[i];
            		}
            	}
            	return null;
            }
 		
 			$scope.removeUser = function(user){
 				var clear = ( $scope.user == user );
 				 
	  			
	  			var url = '/meter/rest/disp.php?m=userremove'  ;
                // var url = "/rest/api/data/devRT?" ;

				url = _appendUrlList( url, "user" , user );
			        
                
                $http.get( url )
                    .success(
                        function(response) 
                        {
                        	response_status( 'remove user :' + user  ,  response );
                            $scope.loadUsers (clear );

                            $scope.change(
			              	 
			              		);
                        }
              );

	  		}

	  	 
	  		
	  		$scope.changeUser = function(user){
	  			$scope.user = user;
	  			$scope.q = '' ;

	  			$scope.loadMeters(
              		$scope.user
              		);

	  		}

	  		
	  		$scope.user_change= function(){
         		 
              	$scope.loadUsers(
              	 
              		);
            }  
         	$scope.change= function(){
         		if( $scope.user )
              	$scope.loadMeters(
              		$scope.user
              		);
            }  

            $scope.loadUsers = function( clear ) {
                
            
 
                var url = '/meter/rest/disp.php?m=userlist'  ;
                url = _appendUrlList( url, "q" , $scope.uq  );  
                $http.get( url )
                    .success(
                        function(response) 
                        {
                        	if( clear  ){
	                        	$scope.user = null;
		  						$scope.q = '' ;
		  						$scope.devices =[];
	                    	}

                            $scope.execResult = response.status ;
                            
                            response_status( 'Loading userlist ' , response );

                            if( response.status != 0 )
                            {
                                return ;
                            }

                            response = JSON.retrocycle(response);
                            $scope.users = response.result;
                           

                        }
              );
            };   

            $scope.updateMeter = function( meter ) {
                
                var cb = $('#checkbox'+meter);
                var checked = cb.is(':checked');
              
                var url = '/meter/rest/disp.php?m=updatemeter'  ;
                // var url = "/rest/api/data/devRT?" ;

				url = _appendUrlList( url, "meter" , meter );
				if( checked )
					url = _appendUrlList( url, "user" ,  $scope.user  );
		            
                
                $http.get( url )
                    .success(
                        function(response) 
                        {
                        	response_status( checked ?  'attach ' + meter +" : "+ $scope.user : 'detach ' + meter +" : "+ $scope.user ,
                        		 response );
                            
                        }
              );
            };   
            $scope.loadMeters = function(user) {
                
              
                var url = '/meter/rest/disp.php?m=meterlist'  ;
                url = _appendUrlList( url, "q" , $scope.q  );
                               
              
   				// url = _appendUrlList( url, "user" ,  user  );
                

                $http.get( url )
                    .success(
                        function(response) 
                        {

                            $scope.execResult = response.status ;
                            response_status( 'Loading meter :' + user  , response );
                            if( response.status != 0 )
                            {
                                return ;
                            }

                            $scope.devices =[];

                            response = JSON.retrocycle(response);

                           	$scope.devices = sortMeters(response.result , user) ;

                        }
              );
            };   

            
                // init 
            $scope.loadUsers(true);
           
  
        });

    function sortMeters( meters  , user ){

    	var mt=  meters.sort(function(one, two){
    		if( user != null ){
	    		if(one._USERID == user )
	    			return -1;
	    		if(two._USERID == user )
	    			return 1;
    		}

    		var m1 = one._METER  ;
    		var m2 = two._METER  ;


    		if( m1 > m2 )
    			return 1;
    		else
    			return -1;
    	});

    	return mt;
    }



    function uploadFiles() {
   		var file = $('input[name="file"').get(0).files[0];
   		if( !file )
   			return ;
		var formData = new FormData();
		formData.append('file', file);

		var htmlv = $('#uploadDiv').html();
		$('#uploadDiv').html(htmlv);
		
		$.ajax({
		  url : '/meter/rest/disp.php?m=upload_users',
		  type : 'POST',
		  data : formData,
		  cache : false,
		  contentType : false,
		  processData : false,
		  success : function( result , textStatus, jqXHR) {
		   // var result = JSON.parse(jqXHR.responseText);
		    // alert(userObj);
		    if(result.status == 0 ){
		    	reloadUser();
		    }else {
		    	alert( result.message );
		    }
		  },
		  error : function(jqXHR, textStatus, errorThrown) {
		    alert(textStatus);
		  }
		});
    }


    function reloadUser(  ){
        var ctrl = angular.element('#deviceCtrl');
        var $scope= ctrl.scope();
 
        $scope.loadUsers(true);
    }



</script>
</html>
